package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hospital.common.core.domain.R;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.HisDocumentSearchBo;
import com.hospital.workstation.domain.vo.HisDocumentSearchVo;
import com.hospital.workstation.domain.HisDocumentSearch;
import com.hospital.workstation.mapper.HisDocumentSearchMapper;
import com.hospital.workstation.service.IHisDocumentSearchService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 检验检查报告调阅Service业务层处理
 *
 * @author whb
 * @date 2023-03-09
 */
@RequiredArgsConstructor
@Service
public class HisDocumentSearchServiceImpl implements IHisDocumentSearchService {

    private final HisDocumentSearchMapper baseMapper;

    /**
     * 查询检验检查报告调阅
     */
    @Override
    public HisDocumentSearchVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询检验检查报告调阅列表
     */
    @Override
    public TableDataInfo<HisDocumentSearchVo> queryPageList(HisDocumentSearchBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HisDocumentSearch> lqw = buildQueryWrapper(bo);
        Page<HisDocumentSearchVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询检验检查报告调阅列表
     */
    @Override
    public List<HisDocumentSearchVo> queryList(HisDocumentSearchBo bo) {
        LambdaQueryWrapper<HisDocumentSearch> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HisDocumentSearch> buildQueryWrapper(HisDocumentSearchBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HisDocumentSearch> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMedicalRecordNo() != null, HisDocumentSearch::getMedicalRecordNo, bo.getMedicalRecordNo());
        lqw.eq(StringUtils.isNotBlank(bo.getDocumentId()), HisDocumentSearch::getDocumentId, bo.getDocumentId());
        lqw.eq(StringUtils.isNotBlank(bo.getDocumentContent()), HisDocumentSearch::getDocumentContent, bo.getDocumentContent());
        return lqw;
    }

    /**
     * 新增检验检查报告调阅
     */
    @Override
    public Boolean insertByBo(HisDocumentSearchBo bo) {
        HisDocumentSearch add = BeanUtil.toBean(bo, HisDocumentSearch.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改检验检查报告调阅
     */
    @Override
    public Boolean updateByBo(HisDocumentSearchBo bo) {
        HisDocumentSearch update = BeanUtil.toBean(bo, HisDocumentSearch.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HisDocumentSearch entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除检验检查报告调阅
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 查看报告
     *
     * @param documentId 文档ID
     * @return
     */
    @Override
    public R<HisDocumentSearchVo> queryReportByDocument(String documentId) {
        // 根据文档ID查询报告
        HisDocumentSearchVo hisDocumentSearchVo = baseMapper.selectVoOne(new LambdaQueryWrapper<HisDocumentSearch>().eq(HisDocumentSearch::getDocumentId, documentId));
        return R.ok(hisDocumentSearchVo);
    }
}
